package com.example.demo.web.entity.mapper;

import com.example.demo.web.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Mapper
@Component
public interface UserMapper {

    @Select("SELECT COUNT(*) FROM users WHERE mobile = #{mobile}")
    Integer mobileExist(@Param("mobile") String mobile);

    @Select("SELECT * FROM users WHERE mobile = #{mobile}")
    User findByMobile(@Param("mobile") String mobile);

    @Insert("INSERT INTO users (mobile, nickname, token, login_at) VALUES(#{mobile}, #{nickname}, #{token}, #{loginAt})")
    int register(@Param("mobile") String mobile, @Param("nickname") String nickname, @Param("token") String token, @Param("loginAt") LocalDateTime loginAt);

    @Update("UPDATE users SET token=#{token}, login_at=#{loginAt} WHERE id=#{id}")
    boolean relogin(@Param("id") Integer id, @Param("token") String token, @Param("loginAt") LocalDateTime loginAt);
}
